/**
 * 
 */
package com.xpec.c4.game.spinMoneyRecorder;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.player.Player;
import com.xpec.c4.game.datamanager.agent.money.CoinType;
import com.xpec.c4.game.spinMoneyRecorder.async.SaveSpinMoneyRecordAsync;
import com.xpec.c4.game.spinMoneyRecorder.db.SpinMoneyRecordLogModel;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.module.BasicServiceModule;

/**
 * slot game 指定 spin n 次後記錄玩家金錢記錄
 * 
 * @author TW15010
 */
public class SpinMoneyRecorder implements Serializable {
	private static final long serialVersionUID = -5615450844720988529L;

	/** log4j object */
	private static final Log log = LogFactory.getLog(SpinMoneyRecorder.class);

	/**
	 * 本次 save 週期內累計 spin 次數
	 */
	private int spinCount;

	/**
	 * 全部累計 spin 次數
	 */
	private int totalSpinCount;

	/**
	 * 每 spin 幾次 save 一次 log
	 */
	private int spinCountForSave;

	/**
	 * 角色#1234
	 */
	private String playerName;

	/**
	 * playerId
	 */
	private String playerId;

	/**
	 * gameType
	 */
	private String gameType;

	/**
	 * hallType
	 */
	private String hallType;

	/**
	 * 機台號碼
	 */
	private int machineNum;

	/**
	 * 起始體驗幣
	 */
	private BigDecimal trialCoinBeg;

	/**
	 * 起始C幣
	 */
	private BigDecimal goldCoinBeg;

	private SpinMoneyRecordLogModel logModel;

	public void initial(Player player, String gameType, String hallType, int machineNum, int spinCountForSave) {
		this.spinCount = 0;
		this.totalSpinCount = 0;
		this.spinCountForSave = spinCountForSave;

		this.playerName = player.getNameWithNumber();
		this.playerId = player.getGuid().toString();
		this.gameType = gameType;
		this.hallType = hallType;
		this.machineNum = machineNum;

		this.trialCoinBeg = player.getTrialCoin();
		this.goldCoinBeg = player.getGoldCoin();
	}

	public void update(BasicServiceModule currentModule, Player player, BigDecimal lastBetMoney, CoinType lastCoinType) {
		spinCount++;
		totalSpinCount++;

		if (spinCount == 1) {
			createNewLogModel();
		}

		Calendar c = Calendar.getInstance();
		logModel.setEventTime(c);
		logModel.setGameTime(TimeSys.currentTimeMills());
		logModel.setEventDate(c.getTime());

		logModel.setSpinCount(totalSpinCount);

		logModel.setTrialCoinEnd(player.getTrialCoin());
		logModel.setGoldCoinEnd(player.getGoldCoin());

		logModel.setLastBetMoney(lastBetMoney);
		logModel.setLastCoinType(lastCoinType.name());

		save(currentModule);

		if (spinCount >= spinCountForSave) {
			spinCount = 0;
			trialCoinBeg = player.getTrialCoin();
			goldCoinBeg = player.getGoldCoin();
		}
	}

	private void createNewLogModel() {
		logModel = new SpinMoneyRecordLogModel();

		logModel.setPlayerName(playerName);
		logModel.setPlayerId(playerId);

		logModel.setGameType(gameType);
		logModel.setHallType(hallType);
		logModel.setMachineNum(machineNum);

		logModel.setSpinCount(totalSpinCount);
		logModel.setSpinCountForSave(spinCountForSave);

		logModel.setTrialCoinBeg(trialCoinBeg);
		logModel.setGoldCoinBeg(goldCoinBeg);
	}

	private void save(BasicServiceModule currentModule) {
		SpinMoneyRecordLogModel cloneModel = null;
		try {
			cloneModel = (SpinMoneyRecordLogModel) logModel.clone();
		} catch (CloneNotSupportedException e) {
			if (log.isErrorEnabled()) {
				log.error("CloneNotSupportedException", e);
			}

			return;
		}

		SaveSpinMoneyRecordAsync task = new SaveSpinMoneyRecordAsync();
		task.setModel(cloneModel);
		task.key = GUID.parseUUID(playerId);

		currentModule.addAsyncCommand(task);
	}
}
